package querylayer

import (
	"database/sql"
	"fmt"
	"os"

	"github.com/tendermint/tendermint/my_app/utils"
	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

func (qe *QueryEngine) insertFileData(fileData *tmproto.FileData) error {

	err := InsertDongRuanDiscription(qe.db, fileData)
	if err != nil {
		return fmt.Errorf("failed to insert file data: %v", err)
	}

	eigenvalueBinary, err := utils.Float32ToBinary(fileData.Eigenvalue)
	if err != nil {
		return fmt.Errorf("failed to convert eigenvalue to binary: %v", err)
	}

	query := `
		INSERT INTO vector_data (
			file_type, file_category, object_id, eigenvalue
		) VALUES ($1, $2, $3, $4)
	`
	_, err = qe.db.Exec(query,
		fileData.FileType,
		fileData.FileCategory,
		fileData.ObjectId,
		eigenvalueBinary,
	)

	if err != nil {
		fmt.Println("failed to insert insert data: %v", err)
		return fmt.Errorf("failed to insert data: %v", err)
	}

	return nil

}

// func (qe *QueryEngine) insertFileData(fileData *tmproto.FileData) error {

// 	eigenvalueBinary, err := utils.Float32ToBinary(fileData.Eigenvalue)
// 	if err != nil {
// 		return fmt.Errorf("failed to convert eigenvalue to binary: %v", err)
// 	}

// 	query := `
// 		INSERT INTO file_data (
// 			file_type, file_category, object_id, owner_id, eigenvalue, ex_method,
// 			name, time, description, data_hash
// 		) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
// 	`
// 	_, err = qe.db.Exec(query,
// 		fileData.FileType,
// 		fileData.FileCategory,
// 		fileData.ObjectId,
// 		fileData.OwnerId,
// 		eigenvalueBinary,
// 		fileData.ExMethod,
// 		fileData.Name,
// 		fileData.Time,
// 		fileData.Description,
// 		fileData.DataHash,
// 	)
// 	fmt.Println(fileData.FileType)
// 	if err != nil {
// 		return fmt.Errorf("failed to insert data: %v", err)
// 	}
// 	return nil
// }

// deleteFileData 从数据库中删除FileData
func (qe *QueryEngine) deleteFileData(fileData *tmproto.FileData) error {
	query := `
		DELETE FROM file_data 
		WHERE file_type = $1 AND file_category = $2 AND object_id = $3
	`
	_, err := qe.db.Exec(query, fileData.FileType, fileData.FileCategory, fileData.ObjectId)
	if err != nil {
		return fmt.Errorf("failed to delete data: %v", err)
	}
	return nil
}

func initializeDatabase(db *sql.DB, tableSchemaFile string) error {

	// 检查 file_data 表是否存在
	query := `SELECT EXISTS (
		SELECT FROM information_schema.tables 
		WHERE table_schema = 'public' AND table_name = 'file_data'
	)`

	var exists bool
	if err := db.QueryRow(query).Scan(&exists); err != nil {
		return fmt.Errorf("failed to check if table exists: %v", err)
	}
	if exists {
		drop := `drop table file_data`
		// 执行语句
		if _, err := db.Exec(string(drop)); err != nil {
			return fmt.Errorf("failed to drop table: %v", err)
		}
		fmt.Println("Table file_data dropped successfully.")
	}

	// 读取建表语句文件
	schema, err := os.ReadFile(tableSchemaFile)
	if err != nil {
		return fmt.Errorf("failed to read schema file: %v", err)
	}

	// 执行建表语句
	if _, err := db.Exec(string(schema)); err != nil {
		return fmt.Errorf("failed to create table: %v", err)
	}

	fmt.Println("Table file_data created successfully.")

	return nil
}

func createUsertable(db *sql.DB) error {
	// 检查 file_data 表是否存在
	query := `CREATE TABLE User_table (
	UserID      VARCHAR(255) PRIMARY KEY,
	Organization VARCHAR(255) NOT NULL,
	Age          INT NOT NULL,
	Gender       VARCHAR(10) NOT NULL,
	Province     VARCHAR(255) NOT NULL
	);`

	// 执行建表语句
	if _, err := db.Exec(query); err != nil {
		return fmt.Errorf("failed to create table: %v", err)
	}

	fmt.Println("Table user_data created successfully.")

	return nil
}
